#pragma once

#include <semaphore.h>
#include <string>

class SharedMemory
{
public:
  enum class Error
  {
    None = 0,
    OpenFileFail,
    MmapFail,
    SemFail
  };
  
  inline static bool IsSuccess(Error ret) {
	  return Error::None == ret;
  }
public:
	SharedMemory();
	SharedMemory(const SharedMemory &) = delete; // forbid copy
	~SharedMemory();

	void Write(const uint8_t *data, uint32_t len);
public:
	Error Init(const char* sharedPath, uint32_t sharedSize, bool semaphore);
private:
	Error CreateSems();
	Error OpenAndInitFile(const char* path, uint32_t sharedSize);
private:
  int fd{-1};
  sem_t *pempty{nullptr};
  sem_t *pfull{nullptr};

  std::string sharedPath;
  uint8_t *ptr{nullptr};
  uint32_t sharedSize{0};
  bool semaphore{false};
};
